package org.fisco.bcos.asset.contract;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.fisco.bcos.sdk.abi.FunctionReturnDecoder;
import org.fisco.bcos.sdk.abi.TypeReference;
import org.fisco.bcos.sdk.abi.datatypes.DynamicArray;
import org.fisco.bcos.sdk.abi.datatypes.DynamicStruct;
import org.fisco.bcos.sdk.abi.datatypes.Function;
import org.fisco.bcos.sdk.abi.datatypes.Type;
import org.fisco.bcos.sdk.abi.datatypes.Utf8String;
import org.fisco.bcos.sdk.abi.datatypes.generated.tuples.generated.Tuple5;
import org.fisco.bcos.sdk.client.Client;
import org.fisco.bcos.sdk.contract.Contract;
import org.fisco.bcos.sdk.crypto.CryptoSuite;
import org.fisco.bcos.sdk.crypto.keypair.CryptoKeyPair;
import org.fisco.bcos.sdk.model.CryptoType;
import org.fisco.bcos.sdk.model.TransactionReceipt;
import org.fisco.bcos.sdk.model.callback.TransactionCallback;
import org.fisco.bcos.sdk.transaction.model.exception.ContractException;

@SuppressWarnings("unchecked")
public class PatientMutiQueries extends Contract {
    public static final String[] BINARY_ARRAY = {"608060405234801561001057600080fd5b50610ada806100206000396000f30060806040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806346e5a56714610051578063f69489eb1461008e575b600080fd5b34801561005d57600080fd5b506100786004803603610073919081019061073c565b6100b7565b60405161008591906109a2565b60405180910390f35b34801561009a57600080fd5b506100b560048036036100b0919081019061077d565b6104ac565b005b60606000826040518082805190602001908083835b6020831015156100f157805182526020820191506020810190506020830392506100cc565b6001836020036101000a0380198251168184511680821785525050505050509050019150509081526020016040518091039020805480602002602001604051908101604052809291908181526020016000905b828210156104a1578382906000526020600020906005020160a06040519081016040529081600082018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102015780601f106101d657610100808354040283529160200191610201565b820191906000526020600020905b8154815290600101906020018083116101e457829003601f168201915b50505050508152602001600182018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102a35780601f10610278576101008083540402835291602001916102a3565b820191906000526020600020905b81548152906001019060200180831161028657829003601f168201915b50505050508152602001600282018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156103455780601f1061031a57610100808354040283529160200191610345565b820191906000526020600020905b81548152906001019060200180831161032857829003601f168201915b50505050508152602001600382018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156103e75780601f106103bc576101008083540402835291602001916103e7565b820191906000526020600020905b8154815290600101906020018083116103ca57829003601f168201915b50505050508152602001600482018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156104895780601f1061045e57610100808354040283529160200191610489565b820191906000526020600020905b81548152906001019060200180831161046c57829003601f168201915b50505050508152505081526020019060010190610144565b505050509050919050565b6104b4610611565b60a0604051908101604052808781526020018681526020018581526020018481526020018381525090506000866040518082805190602001908083835b60208310151561051657805182526020820191506020810190506020830392506104f1565b6001836020036101000a038019825116818451168082178552505050505050905001915050908152602001604051809103902081908060018154018082558091505090600182039060005260206000209060050201600090919290919091506000820151816000019080519060200190610591929190610641565b5060208201518160010190805190602001906105ae929190610641565b5060408201518160020190805190602001906105cb929190610641565b5060608201518160030190805190602001906105e8929190610641565b506080820151816004019080519060200190610605929190610641565b50505050505050505050565b60a06040519081016040528060608152602001606081526020016060815260200160608152602001606081525090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061068257805160ff19168380011785556106b0565b828001600101855582156106b0579182015b828111156106af578251825591602001919060010190610694565b5b5090506106bd91906106c1565b5090565b6106e391905b808211156106df5760008160009055506001016106c7565b5090565b90565b600082601f83011215156106f957600080fd5b813561070c610707826109f1565b6109c4565b9150808252602083016020830185838301111561072857600080fd5b610733838284610a4d565b50505092915050565b60006020828403121561074e57600080fd5b600082013567ffffffffffffffff81111561076857600080fd5b610774848285016106e6565b91505092915050565b600080600080600060a0868803121561079557600080fd5b600086013567ffffffffffffffff8111156107af57600080fd5b6107bb888289016106e6565b955050602086013567ffffffffffffffff8111156107d857600080fd5b6107e4888289016106e6565b945050604086013567ffffffffffffffff81111561080157600080fd5b61080d888289016106e6565b935050606086013567ffffffffffffffff81111561082a57600080fd5b610836888289016106e6565b925050608086013567ffffffffffffffff81111561085357600080fd5b61085f888289016106e6565b9150509295509295909350565b600061087782610a2a565b8084526020840193508360208202850161089085610a1d565b60005b848110156108c95783830388526108ab838351610910565b92506108b682610a40565b9150602088019750600181019050610893565b508196508694505050505092915050565b60006108e582610a35565b8084526108f9816020860160208601610a5c565b61090281610a8f565b602085010191505092915050565b600060a083016000830151848203600086015261092d82826108da565b9150506020830151848203602086015261094782826108da565b9150506040830151848203604086015261096182826108da565b9150506060830151848203606086015261097b82826108da565b9150506080830151848203608086015261099582826108da565b9150508091505092915050565b600060208201905081810360008301526109bc818461086c565b905092915050565b6000604051905081810181811067ffffffffffffffff821117156109e757600080fd5b8060405250919050565b600067ffffffffffffffff821115610a0857600080fd5b601f19601f8301169050602081019050919050565b6000602082019050919050565b600081519050919050565b600081519050919050565b6000602082019050919050565b82818337600083830152505050565b60005b83811015610a7a578082015181840152602081019050610a5f565b83811115610a89576000848401525b50505050565b6000601f19601f83011690509190505600a265627a7a72305820f2832f3cccf86ed82370f943704c313f153136536503bca1346cbf1e59e8678e6c6578706572696d656e74616cf50037"};

    public static final String BINARY = org.fisco.bcos.sdk.utils.StringUtils.joinAll("", BINARY_ARRAY);

    public static final String[] SM_BINARY_ARRAY = {"608060405234801561001057600080fd5b50610ada806100206000396000f30060806040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680632c7250d0146100515780635be21ea31461007a575b600080fd5b34801561005d57600080fd5b506100786004803603610073919081019061077d565b6100b7565b005b34801561008657600080fd5b506100a1600480360361009c919081019061073c565b61021c565b6040516100ae91906109a2565b60405180910390f35b6100bf610611565b60a0604051908101604052808781526020018681526020018581526020018481526020018381525090506000866040518082805190602001908083835b60208310151561012157805182526020820191506020810190506020830392506100fc565b6001836020036101000a03801982511681845116808217855250505050505090500191505090815260200160405180910390208190806001815401808255809150509060018203906000526020600020906005020160009091929091909150600082015181600001908051906020019061019c929190610641565b5060208201518160010190805190602001906101b9929190610641565b5060408201518160020190805190602001906101d6929190610641565b5060608201518160030190805190602001906101f3929190610641565b506080820151816004019080519060200190610210929190610641565b50505050505050505050565b60606000826040518082805190602001908083835b6020831015156102565780518252602082019150602081019050602083039250610231565b6001836020036101000a0380198251168184511680821785525050505050509050019150509081526020016040518091039020805480602002602001604051908101604052809291908181526020016000905b82821015610606578382906000526020600020906005020160a06040519081016040529081600082018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156103665780601f1061033b57610100808354040283529160200191610366565b820191906000526020600020905b81548152906001019060200180831161034957829003601f168201915b50505050508152602001600182018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156104085780601f106103dd57610100808354040283529160200191610408565b820191906000526020600020905b8154815290600101906020018083116103eb57829003601f168201915b50505050508152602001600282018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156104aa5780601f1061047f576101008083540402835291602001916104aa565b820191906000526020600020905b81548152906001019060200180831161048d57829003601f168201915b50505050508152602001600382018054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561054c5780601f106105215761010080835404028352916020019161054c565b820191906000526020600020905b81548152906001019060200180831161052f57829003601f168201915b50505050508152602001600482018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156105ee5780601f106105c3576101008083540402835291602001916105ee565b820191906000526020600020905b8154815290600101906020018083116105d157829003601f168201915b505050505081525050815260200190600101906102a9565b505050509050919050565b60a06040519081016040528060608152602001606081526020016060815260200160608152602001606081525090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061068257805160ff19168380011785556106b0565b828001600101855582156106b0579182015b828111156106af578251825591602001919060010190610694565b5b5090506106bd91906106c1565b5090565b6106e391905b808211156106df5760008160009055506001016106c7565b5090565b90565b600082601f83011215156106f957600080fd5b813561070c610707826109f1565b6109c4565b9150808252602083016020830185838301111561072857600080fd5b610733838284610a4d565b50505092915050565b60006020828403121561074e57600080fd5b600082013567ffffffffffffffff81111561076857600080fd5b610774848285016106e6565b91505092915050565b600080600080600060a0868803121561079557600080fd5b600086013567ffffffffffffffff8111156107af57600080fd5b6107bb888289016106e6565b955050602086013567ffffffffffffffff8111156107d857600080fd5b6107e4888289016106e6565b945050604086013567ffffffffffffffff81111561080157600080fd5b61080d888289016106e6565b935050606086013567ffffffffffffffff81111561082a57600080fd5b610836888289016106e6565b925050608086013567ffffffffffffffff81111561085357600080fd5b61085f888289016106e6565b9150509295509295909350565b600061087782610a2a565b8084526020840193508360208202850161089085610a1d565b60005b848110156108c95783830388526108ab838351610910565b92506108b682610a40565b9150602088019750600181019050610893565b508196508694505050505092915050565b60006108e582610a35565b8084526108f9816020860160208601610a5c565b61090281610a8f565b602085010191505092915050565b600060a083016000830151848203600086015261092d82826108da565b9150506020830151848203602086015261094782826108da565b9150506040830151848203604086015261096182826108da565b9150506060830151848203606086015261097b82826108da565b9150506080830151848203608086015261099582826108da565b9150508091505092915050565b600060208201905081810360008301526109bc818461086c565b905092915050565b6000604051905081810181811067ffffffffffffffff821117156109e757600080fd5b8060405250919050565b600067ffffffffffffffff821115610a0857600080fd5b601f19601f8301169050602081019050919050565b6000602082019050919050565b600081519050919050565b600081519050919050565b6000602082019050919050565b82818337600083830152505050565b60005b83811015610a7a578082015181840152602081019050610a5f565b83811115610a89576000848401525b50505050565b6000601f19601f83011690509190505600a265627a7a72305820a6fc46724424dc25a0ca062172020dbba8599be73ea23f0bc2db3dcbc9514ffd6c6578706572696d656e74616cf50037"};

    public static final String SM_BINARY = org.fisco.bcos.sdk.utils.StringUtils.joinAll("", SM_BINARY_ARRAY);

    public static final String[] ABI_ARRAY = {"[{\"constant\":true,\"inputs\":[{\"name\":\"_id\",\"type\":\"string\"}],\"name\":\"getPatientDataById\",\"outputs\":[{\"components\":[{\"name\":\"id\",\"type\":\"string\"},{\"name\":\"name\",\"type\":\"string\"},{\"name\":\"phone\",\"type\":\"string\"},{\"name\":\"password\",\"type\":\"string\"},{\"name\":\"important\",\"type\":\"string\"}],\"name\":\"\",\"type\":\"tuple[]\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_id\",\"type\":\"string\"},{\"name\":\"_name\",\"type\":\"string\"},{\"name\":\"_phone\",\"type\":\"string\"},{\"name\":\"_password\",\"type\":\"string\"},{\"name\":\"_important\",\"type\":\"string\"}],\"name\":\"storePatientData\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]"};

    public static final String ABI = org.fisco.bcos.sdk.utils.StringUtils.joinAll("", ABI_ARRAY);

    public static final String FUNC_GETPATIENTDATABYID = "getPatientDataById";

    public static final String FUNC_STOREPATIENTDATA = "storePatientData";

    protected PatientMutiQueries(String contractAddress, Client client, CryptoKeyPair credential) {
        super(getBinary(client.getCryptoSuite()), contractAddress, client, credential);
    }

    public static String getBinary(CryptoSuite cryptoSuite) {
        return (cryptoSuite.getCryptoTypeConfig() == CryptoType.ECDSA_TYPE ? BINARY : SM_BINARY);
    }

    public List<Struct0> getPatientDataById(String _id) throws ContractException {
        final Function function = new Function(FUNC_GETPATIENTDATABYID, 
                Arrays.<Type>asList(new org.fisco.bcos.sdk.abi.datatypes.Utf8String(_id)), 
                Arrays.<TypeReference<?>>asList(new TypeReference<DynamicArray<Struct0>>() {}));
        return executeCallWithSingleValueReturn(function, List.class);
    }

    public TransactionReceipt storePatientData(String _id, String _name, String _phone,
            String _password, String _important) {
        final Function function = new Function(
                FUNC_STOREPATIENTDATA, 
                Arrays.<Type>asList(new org.fisco.bcos.sdk.abi.datatypes.Utf8String(_id), 
                new org.fisco.bcos.sdk.abi.datatypes.Utf8String(_name), 
                new org.fisco.bcos.sdk.abi.datatypes.Utf8String(_phone), 
                new org.fisco.bcos.sdk.abi.datatypes.Utf8String(_password), 
                new org.fisco.bcos.sdk.abi.datatypes.Utf8String(_important)), 
                Collections.<TypeReference<?>>emptyList());
        return executeTransaction(function);
    }

    public byte[] storePatientData(String _id, String _name, String _phone, String _password,
            String _important, TransactionCallback callback) {
        final Function function = new Function(
                FUNC_STOREPATIENTDATA, 
                Arrays.<Type>asList(new org.fisco.bcos.sdk.abi.datatypes.Utf8String(_id), 
                new org.fisco.bcos.sdk.abi.datatypes.Utf8String(_name), 
                new org.fisco.bcos.sdk.abi.datatypes.Utf8String(_phone), 
                new org.fisco.bcos.sdk.abi.datatypes.Utf8String(_password), 
                new org.fisco.bcos.sdk.abi.datatypes.Utf8String(_important)), 
                Collections.<TypeReference<?>>emptyList());
        return asyncExecuteTransaction(function, callback);
    }

    public String getSignedTransactionForStorePatientData(String _id, String _name, String _phone,
            String _password, String _important) {
        final Function function = new Function(
                FUNC_STOREPATIENTDATA, 
                Arrays.<Type>asList(new org.fisco.bcos.sdk.abi.datatypes.Utf8String(_id), 
                new org.fisco.bcos.sdk.abi.datatypes.Utf8String(_name), 
                new org.fisco.bcos.sdk.abi.datatypes.Utf8String(_phone), 
                new org.fisco.bcos.sdk.abi.datatypes.Utf8String(_password), 
                new org.fisco.bcos.sdk.abi.datatypes.Utf8String(_important)), 
                Collections.<TypeReference<?>>emptyList());
        return createSignedTransaction(function);
    }

    public Tuple5<String, String, String, String, String> getStorePatientDataInput(
            TransactionReceipt transactionReceipt) {
        String data = transactionReceipt.getInput().substring(10);
        final Function function = new Function(FUNC_STOREPATIENTDATA, 
                Arrays.<Type>asList(), 
                Arrays.<TypeReference<?>>asList(new TypeReference<Utf8String>() {}, new TypeReference<Utf8String>() {}, new TypeReference<Utf8String>() {}, new TypeReference<Utf8String>() {}, new TypeReference<Utf8String>() {}));
        List<Type> results = FunctionReturnDecoder.decode(data, function.getOutputParameters());
        return new Tuple5<String, String, String, String, String>(

                (String) results.get(0).getValue(), 
                (String) results.get(1).getValue(), 
                (String) results.get(2).getValue(), 
                (String) results.get(3).getValue(), 
                (String) results.get(4).getValue()
                );
    }

    public static PatientMutiQueries load(String contractAddress, Client client,
            CryptoKeyPair credential) {
        return new PatientMutiQueries(contractAddress, client, credential);
    }

    public static PatientMutiQueries deploy(Client client, CryptoKeyPair credential) throws
            ContractException {
        return deploy(PatientMutiQueries.class, client, credential, getBinary(client.getCryptoSuite()), "");
    }

    public static class Struct0 extends DynamicStruct {
        public String id;

        public String name;

        public String phone;

        public String password;

        public String important;

        public Struct0(Utf8String id, Utf8String name, Utf8String phone, Utf8String password,
                Utf8String important) {
            super(id,name,phone,password,important);
            this.id = id.getValue();
            this.name = name.getValue();
            this.phone = phone.getValue();
            this.password = password.getValue();
            this.important = important.getValue();
        }

        public Struct0(String id, String name, String phone, String password, String important) {
            super(new org.fisco.bcos.sdk.abi.datatypes.Utf8String(id),new org.fisco.bcos.sdk.abi.datatypes.Utf8String(name),new org.fisco.bcos.sdk.abi.datatypes.Utf8String(phone),new org.fisco.bcos.sdk.abi.datatypes.Utf8String(password),new org.fisco.bcos.sdk.abi.datatypes.Utf8String(important));
            this.id = id;
            this.name = name;
            this.phone = phone;
            this.password = password;
            this.important = important;
        }
    }
}
